package com.meshnetics.gb.stack.mac.type;

import com.meshnetics.gb.type.impl.zcl.Bitmap;

/**
 * GTS characteristics bitmap. Used in MACGtsRequest, MACGtsConfirm and MACGtsIndication.
 *
 * @author AKovylin
 */
public class GTSCharacteristics extends Bitmap {
    public GTSCharacteristics(int gtsLength, int gtsDirection, int characteristicsType) {
        this(TypeGTSCharacteristics.TYPE, new byte[]{0});
        if (gtsLength < 0 || gtsLength > 15) {
            throw new IllegalArgumentException("invalid gtsLength");
        }
        if (gtsDirection != 0 && gtsDirection != 1) {
            throw new IllegalArgumentException("invalid gtsDirection");
        }
        if (characteristicsType != 0 && characteristicsType != 1) {
            throw new IllegalArgumentException("invalid characteristicsType");
        }
        super.setBits(0, 4, gtsLength);
        super.setBit(4, gtsDirection == 1);
        super.setBit(5, characteristicsType == 1);
    }

    GTSCharacteristics(TypeGTSCharacteristics type, byte[] value) {
        super(type, value);
        if (value == null) {
            throw new NullPointerException("value is null");
        }
        if (value.length != 1) {
            throw new IllegalArgumentException("invalid value");
        }
        if (getBit(6) || getBit(7)) {
            throw new IllegalArgumentException("reserved bit(s) is(are) set");
        }
    }

    public int getGtsLength() {
        return getBits(0, 4);
    }

    public int getGtsDirection() {
        return getBit(4) ? 1 : 0;
    }

    public int getCharacteristicsType() {
        return getBit(5) ? 1 : 0;
    }
}
